package com.app.micaihu.view.utils

import com.app.micaihu.bean.news.ShareBean
import com.app.micaihu.configure.UmEventId
import com.app.micaihu.utils.MobclickAgentUtils
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.Utils
import com.umeng.socialize.ShareAction
import com.umeng.socialize.UMShareAPI
import com.umeng.socialize.UMShareListener
import com.umeng.socialize.bean.SHARE_MEDIA
import com.umeng.socialize.media.UMImage
import com.umeng.socialize.media.UMWeb

class ShareUtils private constructor() {

    companion object {
        val instance: ShareUtils by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
            ShareUtils()
        }
    }

    //分享回调监听
    var umShareListener: UMShareListener = object : UMShareListener {
        override fun onStart(share_media: SHARE_MEDIA) {

        }

        override fun onResult(platform: SHARE_MEDIA) {
        }

        override fun onError(platform: SHARE_MEDIA, t: Throwable) {
        }

        override fun onCancel(platform: SHARE_MEDIA) {
        }
    }

    fun shareWx(shareBean: ShareBean, type: SHARE_MEDIA, umShareListener: UMShareListener = this.umShareListener) {
        val installApk = UMShareAPI.get(ActivityUtils.getTopActivity()).isInstall(ActivityUtils.getTopActivity(), type) //判断是否本地有客户端
        if (!installApk) {
            com.app.utils.util.ToastUtils.makeText("请先安装客户端")
            return
        }
        when (type) {
            SHARE_MEDIA.QQ -> {
                MobclickAgentUtils.event(UmEventId.articleshare_click, UmEventId.shareQQ)
            }
            SHARE_MEDIA.WEIXIN -> {
                MobclickAgentUtils.event(UmEventId.articleshare_click, UmEventId.shareWx)
            }
            SHARE_MEDIA.SINA -> {
                MobclickAgentUtils.event(UmEventId.articleshare_click, UmEventId.shareWeibo)
            }
            else -> {
            }
        }
        val urlImage = UMWeb(shareBean.shareUrl)
        urlImage.title = shareBean.getShareTitle(null)
        urlImage.description = shareBean.shareSummary
        urlImage.setThumb(UMImage(Utils.getApp(), shareBean.shareImg))
        ShareAction(ActivityUtils.getTopActivity()).setPlatform(type).setCallback(umShareListener)
                .withMedia(urlImage)
                .share()
    }
}